from selenium.webdriver.common.by import By
from frp_page_object.base_page import BasePage, logger
from time import sleep
from selenium.webdriver.support.ui import Select
from selenium.common import NoSuchElementException


class NewRetailOrderPage(BasePage):
    loc_new_order_button = (By.XPATH, '//*[contains(text(),"新增")]')
    loc_store = (By.XPATH, '//*[@id="salOrderManageAdd"]//label[contains(text(),"门店")]/..//input['
                           '@placeholder="请选择"]')
    loc_marketer = (By.XPATH, '//*[@id="salOrderManageAdd"]//label[contains(text(),"销售人员")]/..//*[contains(text(),'
                              '"请选择")]')
    loc_sales_policy = (By.XPATH, '//*[@id="salOrderManageAdd"]//label[contains(text(),"销售政策")]/..//input['
                                  '@placeholder="请选择"]')
    loc_phone_number = (By.XPATH, '//*[@id="salOrderManageAdd"]//label[contains(text(),"顾客手机")]/..//input')
    loc_customer_name = (By.XPATH, '//*[@id="salOrderManageAdd"]//label[contains(text(),"顾客名称")]/..//input')
    loc_customer_delivery = (By.XPATH, '//*[@id="salOrderManageAdd"]//label[contains(text(),"顾客交期")]/..//input')
    loc_address = (By.XPATH, '//*[@id="salOrderManageAdd"]//label[contains(text(),"详细地址")]/..//input')
    loc_package = (By.XPATH, '//*[@id="salOrderManageAdd"]//label[contains(text(),"是否夺冠套餐引流")]/..//input/..')
    loc_package_items = (By.XPATH, '//*[@id="salOrderManageAdd"]//label[contains(text(),"是否夺冠套餐引流")]/../div')
    loc_new_product_button = (By.XPATH, '//*[@id="salOrderManageEdit"]//*[contains(text(),"新增")]')
    loc_input_liaohao = (By.XPATH, '//*[@id="queryItemManagerList"]//label[contains(text(),"料号")]/..//input')
    loc_search_button = (By.XPATH, '//*[@id="queryItemManagerList"]//button[1]')
    loc_select_button = (By.XPATH, '//*[@id="gview_productManagerListTable"]//button[1]')
    loc_save_button = (By.XPATH, '//*[@id="salOrderManageEdit"]//*[contains(text(),"保存")]')
    loc_submit_button = (By.XPATH, '//*[@id="salOrderManageEdit"]//*[contains(text(),"提交")]')
    loc_order_number = (By.XPATH, '//*[@aria-describedby="productColModel_orderNo"]')
    loc_audit = (By.XPATH, '//div[@id="salOrderManageEdit"]//button//*[contains(text(),"审核")]')

    def click_new_product_button(self):
        self.click_wait(self.loc_new_product_button, 10)

    def select_store(self, store_name):
        self.input(self.loc_store, store_name)
        try:
            self.click((By.XPATH, f'//div[@data-transfer="true"]//li[contains(text(),"{store_name}")]'), 5)
        except NoSuchElementException:
            logger.error(f'找不到门店：{store_name}')

    def click_marketer(self, marketer_name):
        loc_marketer_item = (By.XPATH, f'//li[contains(text(),"{marketer_name}")]/..')
        self.click_text_in(marketer_name, self.loc_marketer, loc_marketer_item)

    def input_phone_number(self, number):
        self.input(self.loc_phone_number, number)

    def input_customer_name(self, name):
        self.input(self.loc_customer_name, name)

    def input_customer_delivery(self, date):
        self.input(self.loc_customer_delivery, date)

    def input_address(self, address):
        self.input(self.loc_address, address)

    def click_package(self, yon):
        self.click_text_in(yon, self.loc_package, self.loc_package_items)

    def input_all_new_order(self, store_name, marketer_name, number, customer_name, date, address, yon):
        self.click_wait(self.loc_new_order_button)
        self.select_store(store_name)
        self.click_marketer(marketer_name)
        self.input_phone_number(number)
        self.input_customer_name(customer_name)
        self.input_customer_delivery(date)
        self.input_address(address)
        self.click_package(yon)
        self.click_new_product_button()

    def new_product(self, liaohao):
        self.input(self.loc_input_liaohao, liaohao)
        self.click_wait(self.loc_search_button)
        self.double_click((By.XPATH, f'//*[@id="gview_productManagerListTable"]//*[contains(text(),"{liaohao}")]'))

    # 目前料号搜索功能还没开发，后期合约改为搜索
    def new_product_stocks(self, liaohao):
        self.click((By.XPATH, '//*[contains(text(),"库存商品")]'))
        self.double_click((By.XPATH, f'//*[text()="{liaohao}"]'))

    # 目前自建料品价格没有维护，需要选为赠品。后期可以直接用 new_product(self, liaohao):
    def new_product_self_build(self, liaohao):
        self.new_product(liaohao)
        self.click((By.XPATH, '//*[@id="productColModel"]//tr[2]'))
        select = Select(self.locator((By.XPATH, '//*[@id="productColModel"]//select'), 2))
        select.select_by_visible_text("赠品")

    def new_product_suite(self, liaohao, suite_names):
        self.new_product(liaohao)
        self.click((By.XPATH, '//*[@id="productColModel"]//tr[2]'))
        self.click((By.XPATH, '//*[@id="productColModel"]//*['
                              '@aria-describedby="productColModel_itemSpecsWithSmallSpec"]//a'))
        for name in suite_names:
            self.click((By.XPATH, f'//*[@id="basisItemItemConfigList"]//*[contains(text(),"{name}")]'))
        self.click((By.XPATH, '//*[@id="basisItemItemConfigList"]//*[contains(text(),"确认")]'), 8)

    def submit(self):
        self.click_wait(self.loc_save_button, 5)
        # 商品齐配
        try:
            self.click((By.XPATH, '//*[contains(text(),"商品齐配")]/..//button[2]'), 3)
        except NoSuchElementException:
            pass
        self.click_wait(self.loc_submit_button, 10)
        # 全产品线
        try:
            self.click((By.XPATH, '//*[@id="prodLineChosList"]//*[contains(text(),"顾家工艺")]'), 3)
            self.click((By.XPATH, '//*[@id="prodLineChosList"]//button'))
        except NoSuchElementException:
            pass
        # 改交期
        try:
            self.click((By.XPATH, '//*[contains(text(),"继续提交")]'), 3)
        except NoSuchElementException:
            pass
        # 加急
        try:
            self.click((By.XPATH, '//div[@class="layui-layer layui-layer-dialog "]//a[1]'), 2)
        except NoSuchElementException:
            pass

        order_number = self.locator(self.loc_order_number).get_attribute("title")
        logger.info(f'订单号:    {order_number}')
        return order_number

    def click_audit(self):
        self.click(self.loc_audit)
        # 这个loc可能不稳定
        self.click_wait((By.XPATH, '//div[@class="ivu-modal-wrap"]//button[2]'), 5)
